package com.jackstraw.dbtool.controller;

import com.jackstraw.dbtool.common.Result;
import com.jackstraw.dbtool.exception.MyException;
import com.jackstraw.dbtool.service.IE2dService;
import com.jackstraw.dbtool.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所在包：com.jackstraw.e2d.controller
 * 文件名：
 * 版权：稻草人.
 * 项目：e2d
 * 功能描述：
 * 修改人：Xinglz
 * 修改时间：2020-01-15 9:41
 */
@RestController
@RequestMapping(value="/e2d")
public class E2dController {

    @Autowired
    private IE2dService e2dService;

    /**
     * 初始化excel
     * @param file
     * @return
     */
    @PostMapping(value="/initExcel")
    public Result<Map<String, Object>> initExcel(MultipartFile file) {
        Result<Map<String, Object>> result = new Result<>();
        ExcelUtil.cleanExcel();
        ExcelUtil.initExcel(file);
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, String>> sheetNameList = new ArrayList<>();
        Map<String, List<Map<String, String>>> titleMap = new HashMap<>();
        Workbook workBook = e2dService.getWorkBook(file);
        int sheetNums = workBook.getNumberOfSheets();
        for (int i=0; i<sheetNums; i++) {
            Map<String, String> sheetNameMap = new HashMap<>();
            // 获取工作簿和表头行
            Sheet sheet = workBook.getSheetAt(i);
            Row row = sheet.getRow(0);
            // 封装数据
            if (row != null) {
                String sheetName = sheet.getSheetName();
                sheetNameMap.put("label", sheetName);
                sheetNameMap.put("value", sheetName);
                sheetNameList.add(sheetNameMap);
                List<Map<String, String>> titleList = new ArrayList<>();
                for (Cell cell : row) {
                    Map<String, String> titleNameMap = new HashMap<>();
                    String title = e2dService.getCellValue(cell);
                    titleNameMap.put("label", title);
                    titleNameMap.put("value", title);
                    titleList.add(titleNameMap);
                }

                titleMap.put(sheetName, titleList);
            }
        }
        resultMap.put("sheetList", sheetNameList);
        resultMap.put("titleListMap", titleMap);
        result.setData(resultMap);
        result.success();
        return result;
    }

    /**
     * 将excel数据导入到数据库
     * @param sheetName
     * @param titleList
     * @param tableName
     * @return
     */
    @RequestMapping(value="/importData")
    public Result<Object> uploadExcel(String sheetName ,String titleList, String tableName) {
        Result<Object> result = new Result<>();
        try {
            e2dService.excelToDB(sheetName, titleList, tableName);
            result.success();
        } catch (MyException e) {
            e.printStackTrace();
            result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result.error("系统错误，请联系管理员！");
        }
        return result;
    }
}
